package com.bookcity.front.webfront.vo;

import com.bookcity.front.webfront.config.MyStore;
import com.bookcity.front.webfront.domain.Member;
import com.bookcity.front.webfront.mapper.MemberMapper;
import com.bookcity.front.webfront.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * @author oldliu
 * @since 1.0
 */
@Component
public class MemberService implements UserDetailsService {
    @Autowired
    private MyStore redisTemplate;

    @Autowired
    private MemberMapper memberService;
    @Override
    public UserDetails loadUserByUsername(String accountname) throws UsernameNotFoundException {
        Member member = memberService.selectByAccoutname(accountname);
        Set<GrantedAuthority> authorities=new HashSet<>();
        authorities.add(new GrantedAuthority(){

            @Override
            public String getAuthority() {
                return "douyinuser";
            }
        });
        return new MemberInfo(member,authorities);
    }

    public UserDetails loadByDouyinid(String did) throws UsernameNotFoundException {
        Member member = memberService.getByOpenid(did);
        Set<GrantedAuthority> authorities=new HashSet<>();
        authorities.add(new GrantedAuthority(){

            @Override
            public String getAuthority() {
                return "douyinuser";
            }
        });
        return new MemberInfo(member,authorities);
    }
}
